package com.qumitech.visualnovel.service.retain.impl;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.retain.RetainConfigVO;
import com.qumitech.visualnovel.datasource.console.RetainConfigMapper;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.pubdef.dto.retain.RetainConfigDTO;
import com.qumitech.visualnovel.service.retain.RetainConfigService;
import com.google.common.collect.ImmutableMap;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Service
public class RetainConfigServiceImpl implements RetainConfigService {

    @Autowired
    RetainConfigMapper retainConfigMapper;

//    ImmutableMap<Integer, String> retainWaysMap = ImmutableMap.of(0, "触发", 1, "每天", 2, "每周", 3, "每月");

    @Override
    public List<RetainConfigVO> listRetainConfig(String name, String grouping, Integer status, String createTime, String orderBy, String orderType, int offset, int limit) {
        return retainConfigMapper.listRetainConfig(name, grouping, status, createTime, orderBy, orderType, offset, limit);
    }

    @Override
    public int countRetainConfig(String name, String grouping, Integer status, String createTime) {
        return retainConfigMapper.countRetainConfig(name,grouping, status, createTime);
    }

    @Override
    public BaseRsp updateRetainConfig(RetainConfigDTO retainConfigDTO) {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpSession rs = request.getSession();
        SimplePrincipalCollection userinfo = (SimplePrincipalCollection) rs.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY");
        UserInfo u = (UserInfo) userinfo.getPrimaryPrincipal();
        String username = u.getUsername();
        retainConfigDTO.setCreatedBy(username);
        Integer id =  retainConfigDTO.getId();
        if (id == null) {
            String retainName = retainConfigDTO.getName();
            if(countRetainConfig(retainName, null, 1, null) !=0) {
                return new BaseRsp(401, "场景名称重复");
            }
            retainConfigMapper.createRetainConfig(retainConfigDTO);
        } else {
            retainConfigMapper.updateRetainConfig(retainConfigDTO);
        }
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @Override
    public void deleteRetainConfig(Integer id) {
        if (id == null) {
            return;
        }
        retainConfigMapper.deleteRetainConfig(id);
    }

    @Override
    public void ModifyRetainConfigStatus(RetainConfigDTO retainConfigDTO) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpSession rs = request.getSession();
        SimplePrincipalCollection userinfo = (SimplePrincipalCollection) rs.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY");
        UserInfo u = (UserInfo) userinfo.getPrimaryPrincipal();
        String username = u.getUsername();
        retainConfigDTO.setCreatedBy(username);
        retainConfigMapper.modifyRetainConfigStatus(retainConfigDTO);
    }

    @Override
    public List<HashMap<String, Object>> listRetainGroupings() {
        return retainConfigMapper.listRetainGroupings();
    }
}
